using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using DigitalFortress.Entities;
using System.ServiceModel;
using DigitalFortress.Configuration;
using DigitalFortress.Fault;
using Castle.ActiveRecord;

namespace DigitalFortress.Testing.Agent.Administration.ApplicationOperations {

	/// <summary>
	/// Description 
	/// This method is responsible for retrieving the applications in the security repository, in the given page with the given page size.
	/// This operation is only allowed for DigitalFortress.
	/// 
	/// Parameters 
	/// Int32 currentPage - Current page.
	/// Int32 pageSize - The number of items to return for the current page. 
	/// Returns 
	/// Application[] - Returns an array of the roles in the current application, in the given page. 
	/// Dependencies 
	/// 2.1 - Test successful creation. 
	/// Test Suite: 
	/// 1) Test successful retrieval with at least 5 applications. Retrieve currentPage 1 with pageSize 3, and currentPage 2 with pageSize 3. In the first query the return must containt 3 applications, in the second query the return must contain 2 applications.
	/// 2) Test retrieval with pageSize equal to 0, resulting in an expected fault (InvalidPageSizeFault).
	/// 3) Test retrieval with currentPage equal to 0, resulting in an expected fault (InvalidCurrentPageFault).
	/// 4) Test retrieval of an invalid page. Using the first test case, try to retrieve page 3, resulting in an expected fault (PageOutOfRangeFault).
	/// 5) Test retrieval of the applications logged as an application that's not DigitalFortress, resulting in an expected fault (OperationNotAllowedFault). 
	/// </summary>
	[TestFixture]
	public class RetrieveSlicedApplicationsTestSuite : BaseTestFixture {

		#region Fields
		AdministrationAgent agent = null;
		#endregion Fields

		#region Class Init

		/// <summary>
		/// Setups the test fixture.
		/// </summary>
		[TestFixtureSetUp]
		public override void TestFixtureSetUp() {
			base.TestFixtureSetUp();
			agent = new AdministrationAgent(XmlConfiguration.Current);
		}

		#endregion Class Init

		#region Initialize Data

		/// <summary>
		/// Initializes data for this test.
		/// </summary>
		public override void InitializeData() {

		}

		#endregion Initialize Data

		#region Tests

		/// <summary>
		/// Test successful retrieval with at least 5 applications. 
		/// Retrieve currentPage 1 with pageSize 3, and currentPage 2 with pageSize 3. 
		/// In the first query the return must containt 3 applications, 
		/// in the second query the return must contain 2 applications.
		/// </summary>
		[Test]
		public void RetrieveSlicedApplicationsTest1() {
			Application appSample = ObjectMother.GetSampleApplication();
			appSample = agent.CreateApplication(appSample, "TestPassword");

			appSample = ObjectMother.GetSampleApplication();
			appSample = agent.CreateApplication(appSample, "TestPassword");

			appSample = ObjectMother.GetSampleApplication();
			appSample = agent.CreateApplication(appSample, "TestPassword");

			Application[] app = agent.RetrieveSlicedApplications(1, 3);
			Application[] app2 = agent.RetrieveSlicedApplications(2, 3);

			Assert.AreEqual(3, app.Length);
			Assert.AreEqual(2, app2.Length);
		}

		/// <summary>
		/// Test retrieval with pageSize equal to 0,
		/// resulting in an expected fault (InvalidPageSizeFault).
		/// </summary>
		[Test, ExpectedException(typeof(FaultException))]
		public void RetrieveSlicedApplicationsTest2() {
			agent.RetrieveSlicedApplications(1, 0);
		}

		/// <summary>
		/// Test retrieval with currentPage equal to 0, 
		/// resulting in an expected fault (InvalidCurrentPageFault).
		/// </summary>
		[Test, ExpectedException(typeof(FaultException))]
		public void RetrieveSlicedApplicationsTest3() {
			agent.RetrieveSlicedApplications(0, 1);
		}

		/// <summary>
		/// Test retrieval of an invalid page. Using the first test case, 
		/// try to retrieve page 3, resulting in an expected fault (PageOutOfRangeFault).
		/// </summary>
		[Test, ExpectedException(typeof(FaultException))]
		public void RetrieveSlicedApplicationsTest4() {
			agent.RetrieveSlicedApplications(5, 1);
		}

		/// <summary>
		/// Test retrieval of applications logged as an application that's not DigitalFortress, 
		/// resulting in an expected fault (OperationNotAllowedFault). 
		/// </summary>
		[Test, Ignore("No security implementation so far.")]
		public void RetrieveSlicedApplicationsTest5() {
			Application appSample = ObjectMother.GetSampleApplication();
			agent.CreateApplication(appSample, "TestPassword");

			Application app = GetAlternativeAgent().RetrieveApplicationByName(appSample.Name);
		}
		#endregion Tests

		#region Support Methods

		/// <summary>
		/// Gets the agent logged as an application other than DigitalFortress.
		/// </summary>
		/// <returns>Returns the logged agent</returns>
		private AdministrationAgent GetAlternativeAgent() {
			IConfiguration config = new CustomConfiguration(Guid.Empty, "fakePass");
			AdministrationAgent fakeAgent = new AdministrationAgent(config);

			return fakeAgent;
		}

		#endregion Support Methods

	}
}
